package com.tys.algorithm.advanced.code.class30;

public class Code01_MorrisTraversal {

    public static class Node {
        public int value;
        Node left;
        Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static void process(Node root) {
        if (root == null) {
            return;
        }
        // 1
        process(root.left);
        // 2
        process(root.right);
        // 3
    }

    //morris序
    public static void morris(Node head) {
        if (head == null) {
            return;
        }
        //当前节点
        Node cur = head;
        //左树最右节点
        Node mostRight = null;
        while (cur != null) {
            //cur是否有左树
            mostRight = cur.left;
            if (mostRight != null) {//有左树
                //找到cur左树上最右：向右不为空 && 向右不指向cur就继续向右
                while (mostRight.right != null && mostRight.right != cur) {
                    //向右
                    mostRight = mostRight.right;
                }
                //从while中出来，mostRight是cur左树最右节点
                if (mostRight.right == null) { //mostRight 最右节点为空
                    //最右节点指向cur
                    mostRight.right = cur;
                    //cur向左移
                    cur = cur.left;
                    continue;
                } else {
                    //mostRight.right==cur
                    //指向空
                    mostRight.right = null;
                }
            }
            //cur向右移
            cur = cur.right;
        }
    }

    //先序
    public static void morrisPre(Node head) {
        if (head == null) {
            return;
        }
        Node cur = head;
        Node mostRight = null;
        while (cur != null) {
            mostRight = cur.left;
            if (mostRight != null) {  //有左树
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    //第一次来到的时候打印
                    System.out.print(cur.value + " ");
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    mostRight.right = null;
                }
            } else {
                //没有左树直接打印
                System.out.print(cur.value + " ");
            }
            cur = cur.right;
        }
        System.out.println();
    }

    //中序
    public static void morrisIn(Node head) {
        if (head == null) {
            return;
        }
        Node cur = head;
        Node mostRight = null;
        while (cur != null) {
            mostRight = cur.left;
            if (mostRight != null) {
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                    //第一次继续
                    continue;
                } else {
                    mostRight.right = null;
                }
            }
            //第二次打印
            System.out.print(cur.value + " ");
            cur = cur.right;
        }
        System.out.println();
    }

    //后序：较复杂
    public static void morrisPos(Node head) {
        if (head == null) {
            return;
        }
        Node cur = head;
        Node mostRight = null;
        while (cur != null) {
            mostRight = cur.left;
            if (mostRight != null) {
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    //能到节点两次时
                    mostRight.right = null;
                    //逆序打印左树的右边界
                    printEdge(cur.left);
                }
            }
            cur = cur.right;
        }
        //逆序打印整体的右边界
        printEdge(head);
        System.out.println();
    }

    public static void printEdge(Node head) {
        //链表反转
        Node tail = reverseEdge(head);
        Node cur = tail;
        while (cur != null) {
            //打印
            System.out.print(cur.value + " ");
            cur = cur.right;
        }
        //反转回去
        reverseEdge(tail);
    }

    public static Node reverseEdge(Node from) {
        Node pre = null;
        Node next = null;
        while (from != null) {
            next = from.right;
            from.right = pre;
            pre = from;
            from = next;
        }
        return pre;
    }

    // for test -- print tree
    public static void printTree(Node head) {
        System.out.println("Binary Tree:");
        printInOrder(head, 0, "H", 17);
        System.out.println();
    }

    public static void printInOrder(Node head, int height, String to, int len) {
        if (head == null) {
            return;
        }
        printInOrder(head.right, height + 1, "v", len);
        String val = to + head.value + to;
        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM - lenL;
        val = getSpace(lenL) + val + getSpace(lenR);
        System.out.println(getSpace(height * len) + val);
        printInOrder(head.left, height + 1, "^", len);
    }

    public static String getSpace(int num) {
        String space = " ";
        StringBuffer buf = new StringBuffer("");
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }

    //判断是否是搜索二叉树
    public static boolean isBST(Node head) {
        if (head == null) {
            return true;
        }
        Node cur = head;
        Node mostRight = null;
        Integer pre = null; //上一个节点为空
        boolean ans = true;
        while (cur != null) {
            mostRight = cur.left;
            if (mostRight != null) {
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    mostRight.right = null;
                }
            }
            //上一个节点不为空 && 上一个节点大于等于当前
            if (pre != null && pre >= cur.value) {
                //不是搜索二叉树
                //一定要跑完了才返回，因为涉及到修改二叉树，
                //跑完后二叉树恢复为原来的二叉树
                ans = false;
            }
            //当前设置为前一个
            pre = cur.value;
            cur = cur.right;
        }
        //最后返回
        return ans;
    }

    public static void main(String[] args) {
        Node head = new Node(4);
        head.left = new Node(2);
        head.right = new Node(6);
        head.left.left = new Node(1);
        head.left.right = new Node(3);
        head.right.left = new Node(5);
        head.right.right = new Node(7);
        printTree(head);
        morrisIn(head);
        morrisPre(head);
        morrisPos(head);
        printTree(head);

    }

}
